package com.weibo.view;

import java.util.Date;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.weibo.db.HomeTable;
import com.weibo.sina.R;
import com.weibo.util.MD5;
import com.weibo.util.Util;

public class StatusCursorAdapter extends CursorAdapter {
    private static final String TAG = "StatusCursorAdapter";
	private int mLayout;
	public StatusCursorAdapter(Context context, Cursor c,final int layout) {
		super(context, c);
		mLayout = layout;
		
	}
	

	public StatusCursorAdapter(Context context, Cursor c, boolean autoRequery,final int layout) {
		super(context, c, autoRequery);
		mLayout = layout;
	}


	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
	    //Log.e(TAG, "get new View");
		LayoutInflater inflater = (LayoutInflater) context
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		return inflater.inflate(mLayout, parent, false);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		StatusObj tag = new StatusObj();
		tag.setId(cursor.getLong(cursor.getColumnIndex(HomeTable._ID)));
		tag.setRt_id(cursor.getLong(cursor.getColumnIndex(HomeTable.RTROOTID)));
		String tmpString="";
		view.setTag(tag);
		TextView tmp = (TextView) view.findViewById(R.id.user_name);
		tmpString = cursor.getString(cursor.getColumnIndex(HomeTable.NICK));
		tmp.setText("@" +tmpString);
		tag.setNick(tmpString);
		//Log.e(TAG, "user_name = "+tmp.getText());
		Util.addLink(tmp, true);
		tmp = (TextView) view.findViewById(R.id.post_time);

		
		
		tmp.setText(Util.getPastTime(new Date(cursor.getLong(cursor.getColumnIndex(HomeTable.TIME)))));
		tmp = (TextView) view.findViewById(R.id.content);
		tmp.setTag(tag);
		tmpString=cursor.getString(cursor.getColumnIndex(HomeTable.CONTENT));
		tmp.setText(tmpString);
		tag.setContent(tmpString);
		Util.addLink(tmp, true);
		ImageView imageView =null;
		Drawable imageDrawable=null;
		imageView = (ImageView) view.findViewById(R.id.face);
		String profileImage = cursor.getString(cursor.getColumnIndex(HomeTable.PORTRAIT));
		String key = MD5.getMD5(profileImage.getBytes());
		imageDrawable = Util.loadImage(key, profileImage,context,this);
		if (imageDrawable != null)
			imageView.setBackgroundDrawable(imageDrawable);
		else {
			imageView.setBackgroundResource(R.drawable.face);
		}
		//Log.e(TAG, "rtrootid = "+cursor.getLong(cursor.getColumnIndex(HomeTable.RTROOTID)));
		View t = view.findViewById(R.id.rt_layout);
		long rtrootid = cursor.getLong(cursor.getColumnIndex(HomeTable.RTROOTID));
		if(rtrootid!=0){
			t.setVisibility(View.VISIBLE);
			tmp = (TextView) view.findViewById(R.id.rt_nick);
			tmpString=cursor.getString(cursor.getColumnIndex(HomeTable.RTROOTNICK));
			tmp.setText("@" +tmpString);
			tag.setRt_nick(tmpString);
			Util.addLink(tmp, false);
			tmp = (TextView) view.findViewById(R.id.rt_reason);
			tmpString=cursor.getString(cursor.getColumnIndex(HomeTable.RTREASON));
			tmp.setText(tmpString);
			tag.setRt_reason(tmpString);
			Util.addLink(tmp, false);
			tmp.setTag(tag);
		}else {
			t.setVisibility(View.GONE);
			
		}
		
		String pic = cursor.getString(cursor.getColumnIndex(HomeTable.PIC));
		//Log.e(TAG, "pic = "+pic);
		LinkImageView linkImageView = (LinkImageView) view.findViewById(R.id.image);
		if (pic.trim().length()>0) {
			linkImageView.setImageResource(R.drawable.preview_pic_loading);
			linkImageView.setVisibility(View.VISIBLE);
			linkImageView.setUrl(cursor.getString(cursor.getColumnIndex(HomeTable.OPIC)));
			key = MD5.getMD5(pic.getBytes());
			imageDrawable = Util.loadImage(key, pic,context,this);
			if (imageDrawable != null)
				linkImageView.setImageDrawable(imageDrawable);
		} else {
			linkImageView.setVisibility(View.GONE);
		}
		
		
		
	}
	
	public class StatusObj {
		private long id;
		private long rt_id;
		private String nick;
		private String rt_nick;
		private String content;
		private String rt_reason;
		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}

		public String getNick() {
			return nick;
		}

		public void setNick(String nick) {
			this.nick = nick;
		}

		public String getRt_nick() {
			return rt_nick;
		}

		public void setRt_nick(String rt_nick) {
			this.rt_nick = rt_nick;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}

		public String getRt_reason() {
			return rt_reason;
		}

		public void setRt_reason(String rt_reason) {
			this.rt_reason = rt_reason;
		}

		public long getRt_id() {
			return rt_id;
		}

		public void setRt_id(long rt_id) {
			this.rt_id = rt_id;
		}

		
		
		
		
	}

}
